<?php

namespace app\admin\controller;

use app\models\ArrangingModel;
use app\models\CampusModel;
use app\models\ClassmanageModel;
use app\models\ClassRoomModel;
use app\models\ClassRoomTimeModel;
use app\models\ClassTimeModel;
use app\models\ContractModel;
use app\models\CourseLogModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\GradesModel;
use app\models\HolidayModel;
use app\models\StudentModel;
use app\models\StudentSubjectClassModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\TeacherModel;
use app\models\UserModel;

/**
 * Class Grades
 * @package app\admin\controller
 */
class Grades extends Common
{
    /**
     * 节假日管理模型.
     *
     * @var HolidayModel
     */
    protected $holidayModel;


    /**
     * 学生分班排课管理模型.
     *
     * @var StudentSubjectInfosClassModel
     */
    protected $studentsubjectinfosclassModel;

    /**
     * 学生分班管理模型.
     *
     * @var StudentSubjectClassModel
     */
    protected $studentsubjectclassModel;

    /**
     * 已报课程管理模型.
     *
     * @var CourseLogModel
     */
    protected $courselogModel;

    /**
     * 订单管理模型.
     *
     * @var ContractModel
     */
    protected $contractModel;

    /**

    /**
     * 班级管理列表详情模型.
     *
     * @var GradesDetailsListsModel
     */
    protected $gradesdetaillistModel;
    /**
     * 班级管理详情模型.
     *
     * @var GradesDetailsModel
     */
    protected $gradesdetailModel;
    /**
     * 班级管理模型.
     *
     * @var GradesModel
     */
    protected $gradesModel;

    /**
     * 课程管理模型.
     *
     * @var CourseModel
     */
    protected $courseModel;

    /**
     * 校区管理模型.
     *
     * @var CampusModel
     */
    protected $campusModel;

    /**
     * 教师管理模型.
     *
     * @var TeacherModel
     */
    protected $teacherModel;

    /**
     * 教室管理模型.
     *
     * @var ClassRoomModel
     */
    protected $classRoomModel;

    /**
     * 学员模型.
     *
     * @var StudentModel
     */
    protected $studentModel;

    /**
     * 课时教室管理模型.
     *
     * @var ClassRoomTimeModel
     */
    protected $classRoomTimeModel;

    /**
     * 课时管理模型.
     *
     * @var ClassTimeModel
     */
    protected $classTimeModel;



    /**
     * 排课管理模型.
     *
     * @var ArrangingModel
     */
    protected $arrangingModel;

    /**
     * 班级管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */

    //节假日信息
    protected $holiday_arr = [];


    //排课详情信息
    protected $course_lists_arr = [];



    //已报课程详情信息
    protected $course_assignment_arr = [];

    //课程名称信息表
    protected $course_id_name_arr = [];

    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $param['graduation_status'] = 0;  //未结业
            $result = $this->gradesdetailModel->getClassManageLists($param);
            $this->success('success', '', $result, 0);
        }

        $grades_result = $this->gradesdetailModel->where(['graduation_status' => 0])->select(); //未结业
		$where = [];
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$where['campus_id'] = get_current_campus_id();
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$where['campus_id'] = ['in',get_current_campus_id()];
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$where['status'] = 1;
		$campus_where['status'] = 1;
		$campus = $this->campusModel->where($campus_where)->select()->toArray();
        $course_list = $this->courseModel->where($where)->select(); //课程名称


        /****************教室课表***************/
        $now_day = date("Y-m-d");
        $now_time=strtotime(date("H:i:s"));
		$where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
		$where['class_date'] = $now_day;
        $result = $this->gradesdetaillistModel->where($where)->select();
        $json_room = [];

        $class_where = [];
        $room_id_list = [];
        $room_name_list = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$class_where['campus_id'] = get_current_campus_id();

		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$class_where['campus_id'] = ['in',get_current_campus_id()];
		}
        $class_where['seat_set'] = ['neq',''] ; //是否设置原始排位
        $rooms = $this->classRoomModel->where($class_where)->select();

        foreach ($rooms as $key => $value_room){
            $room_id_list[] = $value_room['id'];
            $room_name_list[] = $value_room['class_name'];
        }

        $room_id = [];
        $gradesdetaillist = [];
        foreach ($result as $item => $value){
            if(!in_array($value['class_room_id'],$room_id)){   //如果不存在
                $room_id[] = $value['class_room_id'];
                $room_status = false;
                $cell_room = [];
                $cell_room['room_id'] = $value['class_room_id']; // 教室ID
                $cell_room['room_name'] = $value['class_room']; //教室名字
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$gradesdetaillist['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$gradesdetaillist['campus_id'] = ['in',get_current_campus_id()];
				}
				$gradesdetaillist['class_date'] = $now_day;
				$gradesdetaillist['class_room_id'] = $value['class_room_id'];
                $result_room = $this->gradesdetaillistModel->where($gradesdetaillist)->select();

                $item_value = [];
                foreach ($result_room as $key => $item){
                    $start_time = strtotime($item['class_subject_start']);  //开始时间
                    $end_time = strtotime($item['class_subject_end']); //结束时间
                    if($now_time >= $start_time && $now_time <= $end_time){  //判断当前时间是否在这个区间内
                        $room_status = true;
                        $item_value = $item;
                        break;
                    }
                }
                if($room_status){
                    $cell_room['teacher'] = $item_value['teacher']; //任课老师
                    $cell_room['room_status'] = 1;  //使用中
                    $cell_room['course_name'] = $this->courseModel->get(['id' => $item_value['course_id'],'campus_id'=>get_current_campus_id()])['course_name'];  //课程名称
                }else{
                    $cell_room['teacher'] = '---'; //任课老师无
                    $cell_room['room_status'] = 0;  //空闲
                    $cell_room['course_name']  = '---'; //无课
                }

                $json_room[] = $cell_room;
            }
        }

        for($i =0; $i < count($room_id_list); $i ++){
            if(!in_array($room_id_list[$i],$room_id)){  //不存在数组中
                $room_id[] = $room_id_list[$i];
                $cell_room = [];
                $cell_room['room_id'] = $room_id_list[$i]; // 教室ID
                $cell_room['room_name'] =  $room_name_list[$i]; //教室名字
                $cell_room['teacher'] = '---'; //任课老师无
                $cell_room['room_status'] = 0;  //空闲
                $cell_room['course_name']  = '---'; //无课
                $json_room[] = $cell_room;
            }

        }

        if(count($room_id) > 0){
            session('room_id', $room_id[0]);
        }
        $json_room_data = json_encode($json_room);
		if (get_current_type() == 4 || get_current_type() == 5) {
			$type = 1;
		} else {
			$type = 0;
		}
        return $this->fetch('',[
        	'course_list' => $course_list,
			'grade' => $grades_result,
			'campus' => $campus,
			'campus_id' => get_current_campus_id(),
			'jsonroom' => $json_room_data,
			'qtype'=> $type
		]);
    }


    public function grades_course(){
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $param['course_id'] = input('course_id'); //当前课程ID
            $param['campus_id'] = input('campus_id');
            $param['graduation_status'] = 0;  //未结业
            $result = $this->gradesdetailModel->getClassManageLists($param);
            $this->success('success', '', $result, 0);
        }
        $course_id = input('course_id');
        $campus_id = input('campus_id');
		$where = [];
		$where['graduation_status'] = 0;
		$where['course_id'] = $course_id;
		$where['campus_id'] = $campus_id;
        $grades_result = $this->gradesdetailModel->where($where)->select(); //未结业
        $campus = $this->campusModel->where(['id'=>$campus_id])->select()->toArray();
        $course_infos = $this->courseModel->get(['id' => $course_id]);//课程
        if($course_infos['student_grade'] != 0){
            $course_infos['student_grade'] = $this->dictionaryModel->get(['id' => $course_infos['student_grade']])['name'];
        }else{
            $course_infos['student_grade'] = '--';
        }
        $course_infos['course_type'] = $this->dictionaryModel->get(['id' => $course_infos['course_type']])['name'];
        if($course_infos['status'] == 1){
            $course_infos['status'] = '有效';
        }else{
            $course_infos['status'] = '无效';
        }

        $course_infos['price'] =  ($course_infos['price'] / 100);
        if($course_infos['describe'] == null){
            $course_infos['describe'] = '--';
        }
        return $this->fetch('',['campus_id'=>$campus_id,'course_id'=>$course_id, 'course_infos' => $course_infos ,'grade' => $grades_result,'campus' => $campus]);

    }


    /*******结业课程****/
    public function end_grades()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $param['graduation_status'] = 1;  //已结业
            $result = $this->gradesdetailModel->getClassManageLists($param);
            $this->success('success', '', $result, 0);
        }
		$where = [];
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$where['campus_id'] = get_current_campus_id();
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$where['campus_id'] = ['in',get_current_campus_id()];
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$where['graduation_status'] = 1;
        $grades_result = $this->gradesdetailModel->where($where)->select();
        $campus = $this->campusModel->where($campus_where)->select()->toArray();
        return $this->fetch('',['grade' => $grades_result,'campus' => $campus]);
    }


    /*******结业课程****/
    public function end_grades_course()
    {

        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $param['graduation_status'] = 1;  //已结业
            $param['course_id'] = input('course_id');
            $param['campus_id'] = input('campus_id');
            $result = $this->gradesdetailModel->getClassManageLists($param);
            $this->success('success', '', $result, 0);
        }
        $course_id = input('course_id');
        $campus_id = input('campus_id');
		$where = [];
		$campus_where = [];
		$campus_where['id'] = $campus_id;
		$campus_where['status'] = 1;
		$where['campus_id'] = $campus_id;
		$where['graduation_status'] = 1;
		$where['course_id'] = $course_id;
        $grades_result = $this->gradesdetailModel->where($where)->select();
        $campus = $this->campusModel->where($campus_where)->select()->toArray();
        return $this->fetch('',['course_id'=> $course_id,'campus_id'=> $campus_id, 'grade' => $grades_result,'campus' => $campus]);
    }


    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function headmaster()
    {
		$where = [];
		$campus_where = [];
		$where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$where['status'] = 1;
		$campus_where['status'] = 1;
        $campus = $this->campusModel->where($campus_where)->select();
        $teacher= $this->teacherModel->where($where)->select();

       //部门
        $department_id_arr = [];
        $teacher_data_department = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['department_id'],$department_id_arr)){
                continue;
            }else{
                $department_id_arr[] = $value['department_id'];
                $teacher_data_department[] = $value;
            }

        }


        //年级
        $grades_id_arr = [];
        $teacher_data_grades = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['grade_id'],$grades_id_arr)){
                continue;
            }else{
                $grades_id_arr[] = $value['grade_id'];
                $teacher_data_grades[] = $value;
            }

        }


        //科目
        $subject_id_arr = [];
        $teacher_data_subject = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['subject_id'],$subject_id_arr)){
                continue;
            }else{
                $subject_id_arr[] = $value['subject_id'];
                $teacher_data_subject[] = $value;
            }

        }



        $this->dictionary();
        return $this->fetch('',[
            'teacher'=>$teacher,
            'campus'=>$campus,
            'teacher_data_department' => $teacher_data_department,
            'teacher_data_grades' => $teacher_data_grades,
            'teacher_data_subject' => $teacher_data_subject,
            'campus_id' => input('campus_id')
        ]);
    }


    public function changeheadmaster()
    {
        $id = input('id');
		$where = [];
		$campus_where = [];
		$where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$where['status'] = 1;
		$campus_where['status'] = 1;
		$campus = $this->campusModel->where($campus_where)->select();
		$teacher= $this->teacherModel->where($where)->select();

		//部门
		$department_id_arr = [];
		$teacher_data_department = [];
		foreach ($teacher as $key=>$value){
			if(in_array($value['department_id'],$department_id_arr)){
				continue;
			}else{
				$department_id_arr[] = $value['department_id'];
				$teacher_data_department[] = $value;
			}
		}

		//年级
		$grades_id_arr = [];
		$teacher_data_grades = [];
		foreach ($teacher as $key=>$value){
			if(in_array($value['grade_id'],$grades_id_arr)){
				continue;
			}else{
				$grades_id_arr[] = $value['grade_id'];
				$teacher_data_grades[] = $value;
			}
		}

		//科目
		$subject_id_arr = [];
		$teacher_data_subject = [];
		foreach ($teacher as $key=>$value){
			if(in_array($value['subject_id'],$subject_id_arr)){
				continue;
			}else{
				$subject_id_arr[] = $value['subject_id'];
				$teacher_data_subject[] = $value;
			}
		}

		$this->dictionary();
		return $this->fetch('',[
			'teacher'=>$teacher,
			'campus'=>$campus,
			'id'=>$id,
			'teacher_data_department' => $teacher_data_department,
			'teacher_data_grades' => $teacher_data_grades,
			'teacher_data_subject' => $teacher_data_subject,
			'campus_id' => input('campus_id')
		]);
    }

    public function editteacher(){

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $data['id'] = $data['id'];
            $data['master_name'] = $data['data']['master_name'];
            $data['master_id'] = $data['data']['master_id'];
            if(!$this->gradesdetailModel->where(['id'=>$data['id']])->update(['headmaster' => $data['master_name'],'user_master_id' => $data['master_id']])){
                $this->error('请不要重复修改', null, -1, 1);
            }else{
                if(!$this->gradesdetaillistModel->where(['class_manage_id'=>$data['id']])->update(['headmaster' => $data['master_name']])){
                    $this->error('修改班主任失败', null, -1, 1);
                }else{
                    $this->success('修改班主任成功', url('Grades/lists'), -1, 1);
                }
            }



        }

    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function teacher()
    {
		$where = [];
		$campus_where = [];
		$where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$where['status'] = 1;
		$campus_where['status'] = 1;
        $campus = $this->campusModel->where($campus_where)->select();
        $teacher= $this->teacherModel->where($where)->select()->toArray();

        //部门
        $department_id_arr = [];
        $teacher_data_department = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['department_id'],$department_id_arr)){
                continue;
            }else{
                $department_id_arr[] = $value['department_id'];
                $teacher_data_department[] = $value;
            }

        }


        //年级
        $grades_id_arr = [];
        $teacher_data_grades = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['grade_id'],$grades_id_arr)){
                continue;
            }else{
                $grades_id_arr[] = $value['grade_id'];
                $teacher_data_grades[] = $value;
            }

        }


        //科目
        $subject_id_arr = [];
        $teacher_data_subject = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['subject_id'],$subject_id_arr)){
                continue;
            }else{
                $subject_id_arr[] = $value['subject_id'];
                $teacher_data_subject[] = $value;
            }

        }


        $this->dictionary();
        return $this->fetch('',[
            'teacher'=>$teacher,
            'campus'=>$campus,
            'teacher_data_department' => $teacher_data_department,
            'teacher_data_grades' => $teacher_data_grades,
            'teacher_data_subject' => $teacher_data_subject,
			'campus_id' => input('campus_id')
        ]);
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function assistant()
    {
		$where = [];
		$campus_where = [];
		$where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$where['status'] = 1;
		$campus_where['status'] = 1;
		$campus = $this->campusModel->where($campus_where)->select();
		$teacher= $this->teacherModel->where($where)->select()->toArray();

        //部门
        $department_id_arr = [];
        $teacher_data_department = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['department_id'],$department_id_arr)){
                continue;
            }else{
                $department_id_arr[] = $value['department_id'];
                $teacher_data_department[] = $value;
            }

        }

        //年级
        $grades_id_arr = [];
        $teacher_data_grades = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['grade_id'],$grades_id_arr)){
                continue;
            }else{
                $grades_id_arr[] = $value['grade_id'];
                $teacher_data_grades[] = $value;
            }

        }


        //科目
        $subject_id_arr = [];
        $teacher_data_subject = [];
        foreach ($teacher as $key=>$value){
            if(in_array($value['subject_id'],$subject_id_arr)){
                continue;
            }else{
                $subject_id_arr[] = $value['subject_id'];
                $teacher_data_subject[] = $value;
            }

        }


        $this->dictionary();
        return $this->fetch('',[
            'teacher'=>$teacher,
            'campus'=>$campus,
            'teacher_data_department' => $teacher_data_department,
            'teacher_data_grades' => $teacher_data_grades,
            'teacher_data_subject' => $teacher_data_subject,
			'campus_id' => input('campus_id')
        ]);
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add_time()
    {
		$where = [];
		$campus_where = [];
		$class_where = [];
		$where['campus_id'] = input('campus_id');
		$class_where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$where['status'] = 1;
		$campus_where['status'] = 1;
		$class_where['status'] = 1;
        $campus = $this->campusModel->where($campus_where)->select();
        $teacher= $this->teacherModel->where($where)->select()->toArray();
        $times= $this->classTimeModel->where(['type'=>2])->select()->toArray();
		$class_where['seat_set'] = ['neq',''] ;
        $rooms = $this->classRoomModel->where($class_where)->select()->toArray();
        $this->dictionary();
        return $this->fetch('',[
            'teacher'=>$teacher,
            'campus'=>$campus,
            'times'=>$times,
            'rooms'=>$rooms,
        ]);
    }


    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add_days()
    {
        $where = [];
		$campus_where = [];
		$class_where = [];
		$where['campus_id'] = input('campus_id');
		$class_where['campus_id'] = input('campus_id');
		$campus_where['id'] = input('campus_id');
		$campus_where['status'] = 1;
		$where['status'] = 1;
		$class_where['status'] = 1;
        $campus = $this->campusModel->where($campus_where)->select();
        $teacher= $this->teacherModel->where($where)->select()->toArray();
        $times= $this->classTimeModel->where(['type'=>2])->select()->toArray();
        $class_where['seat_set'] = ['neq',''] ;
        $rooms = $this->classRoomModel->where($class_where)->select()->toArray();
        $this->dictionary();
        $this->assign('date_value',  input('date'));
        return $this->fetch('',[
            'teacher'=>$teacher,
            'campus'=>$campus,
            'times'=>$times,
            'rooms'=>$rooms,
        ]);
    }



    /**
     * @throws \think\exception\DbException
     */
    public function is_arranging()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $_data = input('post.');
            $data = [];
            $data['teacher'] = $_data['t_teacher_id'] ? $_data['t_teacher_id'] : 0;
            $data['assistant'] = $_data['z_teacher_id'] ? $_data['z_teacher_id'] : 0;
            $data['class_date'] = $_data['class_date'];
            $data['start_date'] = $_data['start_date'];
            $data['class_id'] = $_data['class_id'];
            $data['class_room'] = $_data['class_room'];
            $class_week = $this->getWeek($_data['class_date']);
            $start_week = $this->getWeek($_data['start_date']);
            $time_long = '';
            if($class_week === $start_week){
                $week = $this->classTimeModel->get(['week'=>$class_week])['id'];
                $classRoomTime = $this->classRoomTimeModel->get(['week'=>$week, 'class_id'=>$_data['class_id']]);
                if(!$classRoomTime['time_long']){
                    $this->error($class_week.'没有课', null, '', 1);
                }else{
                    $time_long = $classRoomTime['time_long'];
                }

            }else{
                $startTime = $_data['class_date'];
                $endTime = $_data['start_date'];
                $day = round((strtotime($endTime) - strtotime($startTime))/3600/24);
                for($i=0;$i<$day;$i++){
                    $dayTime = date("Y-m-d", strtotime("$startTime +1 day"));
                    $current_week = $this->getWeek($dayTime);
                    $week = $this->classTimeModel->get(['week'=>$current_week])['id'];
                    $classRoomTime = $this->classRoomTimeModel->get(['week'=>$week,'class_id'=>$_data['class_id']]);
                    if(!$classRoomTime['time_long']){
                        $this->error($class_week.'没有课', null, '', 1);
                    }else{
                        $time_long += $classRoomTime['time_long'];
                    }
                }
            }
            if($time_long >0){
                $this->success('即将排课'.$time_long.'次（'.$time_long.'小时）。是否确定？','', $time_long, 1);
            }
            $this->error('排课为0或者冲突，请重新选择', null, '', 1);

        }
    }

    /**
     * 排课
     * @return mixed
     * @throws \think\exception\DbException
     *
     */
    public function arranging()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $_data = input('post.');
            $data = [];
            $data['teacher'] = $_data['t_teacher_id'] ? $_data['t_teacher_id'] : 0;
            $data['assistant'] = $_data['z_teacher_id'] ? $_data['z_teacher_id'] : 0;
            $data['class_date'] = $_data['class_date'];
            $data['start_date'] = $_data['start_date'];
            $data['class_id'] = $_data['class_id'];
            $data['class_room'] = $_data['class_room'];
            $class_week = $this->getWeek($_data['class_date']);
            $start_week = $this->getWeek($_data['start_date']);
            $time_long = $week = '';
            if($class_week === $start_week){
                $week = $this->classTimeModel->get(['week'=>$class_week])['id'];
                $time_long = $this->classRoomTimeModel->get(['week'=>$week,'class_id'=>$_data['class_id']])['time_long'];
            }else{
                $startTime = $_data['class_date'];
                $endTime = $_data['start_date'];
                $day = round((strtotime($endTime) - strtotime($startTime))/3600/24);
                for($i=0;$i<$day;$i++){
                    $dayTime = date("Y-m-d", strtotime("$startTime +1 day"));
                    $current_week = $this->getWeek($dayTime);
                    $week = $this->classTimeModel->get(['week'=>$current_week])['id'];
                    $time_long = $this->classRoomTimeModel->get(['week'=>$week,'class_id'=>$_data['class_id']])['time_long'];
                }
            }
            $grades = $this->gradesModel->get($data['class_id']);
            $grades_data['already_arranged'] = $time_long;
            if($grades['project_num'] || $grades['project_num']>0){
                $grades_data['project_num'] = $grades['project_num'] - $time_long;
            }
            $data['time_long'] = $time_long;
            $this->gradesModel->save($grades_data,['id'=>$data['class_id']]);
            if (false !== $this->arrangingModel->save($data)) {
                $this->success('排课成功', url('Grades/lists'), '', 1);
            }
            $this->error('排课失败', null, '', 1);
        }
        $where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
		$where['status'] = 1;
        $course = $this->courseModel->where($where)->select()->toArray();
        $teacher_department = $this->teacherModel->where($where)->field('teacher_department')->group('teacher_department')->select()->toArray();
        $teacher_subject = $this->teacherModel->where($where)->field('teacher_subject')->group('teacher_subject')->select()->toArray();
        $teacher_grade = $this->teacherModel->where($where)->field('teacher_grade')->group('teacher_grade')->select()->toArray();
        $class = $this->gradesModel->get(input('id'));
        $class['teacher_name'] = TeacherModel::get($class['teacher'])['teacher_name'];
        return $this->fetch('',[
            'course'=>$course,
            'class'=>$class,
            'department'=>$teacher_department,
            'subject'=>$teacher_subject,
            'grade'=>$teacher_grade,
        ]);
    }

    /**
     * 通过日期获取星期
     */
    public function getByDate()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $date = input('date');
            $res = $this->getWeek($date);
            $this->success('success', '', $res, 1);
        }
    }

    /**
     * 获取星期
     * @param $date
     * @return mixed
     */
    function getWeek($date)
    {
        $datearr = explode("-",$date);     //将传来的时间使用“-”分割成数组
        $year = $datearr[0];       //获取年份
        $month = sprintf('%02d',$datearr[1]);  //获取月份
        $day = sprintf('%02d',$datearr[2]);      //获取日期
        $hour = $minute = $second = 0;   //默认时分秒均为0
        $dayofweek = mktime($hour,$minute,$second,$month,$day,$year);    //将时间转换成时间戳
        $shuchu = date("w",$dayofweek);      //获取星期值
        $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
        return $weekarray[$shuchu];
    }

    /**
     * 添加班级.
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $_data = input('post.');
            $data = [];
            $data['headmaster'] = $_data['b_teacher_id'] ? $_data['b_teacher_id'] : 0;
            $data['teacher'] = $_data['t_teacher_id'] ? $_data['t_teacher_id'] : 0;
            $data['assistant'] = $_data['z_teacher_id'] ? $_data['z_teacher_id'] : 0;
            $data['course_id'] = $_data['course_id'];
            $data['class_name'] = $_data['class_name'];
            $data['preoccupation'] = $_data['preoccupation'];
            $data['class_room'] = $_data['class_room'];
            $data['class_date'] = $_data['class_date'];
            $data['start_date'] = $_data['start_date'];
            $data['project_num'] = $_data['project_num'];
            if (false !== $this->gradesModel->save($data)) {
                $this->success('添加成功', url('Schooltime/binding', ['id' => $this->gradesModel->id]), '', 1);
            }
            $this->error('添加失败', null, '', 1);
        }
		$where = [];
		$add_status = input('add_status');
		$course_id = input('course_id');
		$campus_id = input('campus_id');
		$where['status'] = 1;
		$where['campus_id'] = $campus_id;
        $courses = $this->courseModel->where($where)->select();
        $teacher_department = $this->teacherModel->where($where)->field('teacher_department')->group('teacher_department')->select()->toArray();
        $teacher_subject = $this->teacherModel->where($where)->field('teacher_subject')->group('teacher_subject')->select()->toArray();
        $teacher_grade = $this->teacherModel->where($where)->field('teacher_grade')->group('teacher_grade')->select()->toArray();
        //获取
        $class_room = $this->classRoomModel->where($where)->select()->toArray();
        return $this->fetch('',[
            'course'=>$courses,
            'class_room'=>$class_room,
            'add_status' => $add_status,
            'course_id' => $course_id,
            'campus_id' => $campus_id,
            'department'=>$teacher_department,
            'subject'=>$teacher_subject,
            'grade'=>$teacher_grade,
        ]);
    }

	/**
	 * 选择校区.
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function campus()
	{
		if ($this->request->isAjax()) {
			$campus_id = input('campus');
			$this->success('success', url('Grades/add', ['campus_id' => $campus_id]), '', 0);
		}
		$where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['id'] = ['in',get_current_campus_id()];
		}
		$where['status'] = 1;
		$campus = $this->campusModel->where($where)->select();

		return $this->fetch('', [
			'campus' => $campus
		]);
	}

    /**
     * 修改班级.
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function change_grades()
    {
        $manage_id = input('id');
        $campus_id = input('campus_id');

        /********控制来源***************/
        $source_status = input('status');
        /**********************/
        $grades_list = $this->gradesdetailModel->get(['id' => $manage_id]);
        $gcourse_name = $this->courseModel->get(['id' => $grades_list['course_id']])['course_name'];
        $grades_list['course_name'] = $gcourse_name;
        if($grades_list['preoccupation'] == 0){
            $grades_list['preoccupation'] = '无限';
        }
        $data = [];
        $class_time_place_str = $grades_list['class_time_place'];//字符串

        $room_id_arr = explode(",",$grades_list['room_id']);//教室字符串数组
        $time_slot = explode(",",$grades_list['time_slot']);//时间段字符串数组
        $week_record = explode(",",$grades_list['week_record']);//星期字符串数组
        $room_text = explode(",",$grades_list['room_text']);//教室名称字符串数组

        if($grades_list['repeat_status'] == 1){ //按周
            for($index =0; $index < count($room_id_arr); $index ++){


                $item['weekday'] = $week_record[$index];
                $time_arr = explode("-",$time_slot[$index]);
                $item['start_time'] = $time_arr[0];
                $item['end_time'] = $time_arr[1];
                $item['room_text'] = $room_text[$index];
                $item['room_id'] = $room_id_arr[$index];

                /*
                if(get_current_campus_id() && (1 != get_current_group_id())) {  //非超管
                    $item['room_id'] = ClassRoomModel::get(['campus_id' => get_current_campus_id(),'class_name' => $class])['id'];
                }else{  //超管
                    $item['room_id'] = ClassRoomModel::get(['class_name' => $class])['id'];
                }
                */

                $data[] = $item;
            }

         }else if($grades_list['repeat_status'] == 2){  //按日


            $item['weekday'] = $week_record[0];
            $time_arr = explode("-",$time_slot[0]);
            $item['start_time'] = $time_arr[0];
            $item['end_time'] = $time_arr[1];
            $item['room_text'] = $room_text[0];
            $item['room_id'] = $room_id_arr[0];

            /*
            if(get_current_campus_id() && (1 != get_current_group_id())) {  //非超管
                $item['room_id'] = ClassRoomModel::get(['campus_id' => get_current_campus_id(),'class_name' => $class])['id'];
            }else{  //超管
                $item['room_id'] = ClassRoomModel::get(['class_name' => $class])['id'];
            }
            */
            $data[] = $item;
        }
        $json = json_encode($data);

		$where = [];
		$where['campus_id'] = $campus_id;
		$where['status'] = 1;
        $courses = $this->courseModel->where($where)->select();
        $teacher_department = $this->teacherModel->where($where)->field('teacher_department')->group('teacher_department')->select()->toArray();
        $teacher_subject = $this->teacherModel->where($where)->field('teacher_subject')->group('teacher_subject')->select()->toArray();
        $teacher_grade = $this->teacherModel->where($where)->field('teacher_grade')->group('teacher_grade')->select()->toArray();
        //获取
        $class_room = $this->classRoomModel->where($where)->select()->toArray();
        return $this->fetch('',[
            'course'=>$courses,
            'class_room'=>$class_room,
            'department'=>$teacher_department,
            'jsonstr' => $json,
            'source_status' => $source_status,
            'manage_id' => $manage_id,
            'campus_id' => $campus_id,
            'subject'=>$teacher_subject,
            'grades_list' => $grades_list,
            'grade'=>$teacher_grade,
        ]);
    }

    /***
    *
     * 把班级插入数据库
     **/

    public function addclassdata(){

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input();
            //获取节假日信息
            if(count($this->holiday_arr) <= 0){
                $campus_id = $this->courseModel->get(['id' => $data['course_id']])['campus_id'];
                $company_id = $this->campusModel->get(['id' => $campus_id])['company_id'];  //系统ID
                $result_msg = $this->holidayModel->where(['company_id'=>$company_id])->select();
                foreach ($result_msg as $key=>$value){
                    $this->holiday_arr[] = $value['date_name'];
                }
            }
            $status_num = input('status');
            $conflict =  input('conflict');
            //修改逻辑
            $modify_status = false;
            $manage_id = -1000;
            if(isset($_GET["manage_id"])){//是否存在"id"的参数
                $manage_id = input('manage_id');  //班级id
                $modify_status = true;
            }

            $course_id = $data['course_id'];  //课程id
            $holiday_status = $data['holiday_status']; //是否跳过节假日
            $grades_name = $data['grades_name'];//班级名称
            $headmaster = $data['headmaster'];//班主任
            $preoccupation = $data['preoccupation'];//预招学生
            $teacher = $data['teacher'];//任课老师
            $assistant = $data['assistant'];//助教
            $class_start_date = $data['class_start_date'];//计划开班
            $class_end_date = $data['class_end_date'];//计划结业
            $remarks = $data['remarks'];//备注
            $repeat_status = $data['repeat_status'];//重复类型；1：按周重复，2：按日重复
            $project_num = $data['project_num'];//计划课次
            $interval_value = $data['interval_value'];//重复间隔值：周重复则按根据interval_value的值来间隔重复;日重复则根据interval_value值间隔重复；默认间隔值为0
            $user_master_id = $data['user_master_id']; //班主任ID
            $teacher_id = $data['teacher_id']; //任课老师ID
            $assistant_id = $data['assistant_id']; //助教ID
            $res_teacher = UserModel::get(['teacher_id' => $teacher_id, 'group_type' => 2]);
            $user_teacher_id = $res_teacher ? $res_teacher['id'] : null;
            $res_assistant = UserModel::get(['teacher_id' => $assistant_id, 'group_type' => 3]);
            $user_assistant_id = $res_assistant ? $res_assistant['id'] : null;

            $campus_id = $data['campus_id']; //
            $classdata = $data['classdata'];  //排课数据
            $json_str = json_decode($classdata, true);
            if(count($json_str) <= 0){
                    $this->error('请选择上课时间', null, -1, 1);
            }

            $str = "";
            $weekday_arr = [];
            $room_text_str = "";
            $room_id_str = "";
            $time_slot = "";
            $week_record = "";
            $start_day_class = "";
            $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); //先定义一个数组
            $start_postion = date("w",strtotime($class_start_date));
            for($i = 0 ; $i < count($json_str); $i ++){
                $item = $json_str[$i];
                $weekday = "";
                $weekday_mysql = "";
                if($repeat_status == 2){  //按日
                    $start_day_class = $item['weekday'];
                    $weekday = $weekarray[date("w",strtotime($item['weekday']))];
                }else if ($repeat_status == 1){  //按周
                    $weekday = $item['weekday'];
                    $weekday_mysql =  $item['weekday'];
                }
                $start_time = $item['start_time'];
                $end_time = $item['end_time'];
                $room_text = $item['room_text'];
                $room_id = $item['room_id'];
                if($repeat_status == 2){  //按日
                    $weekday = $item['weekday'].'--每隔'.$interval_value.'日';
                    $weekday_mysql = $item['weekday'];
                }
                $str = $str."【".$weekday.'--'.$start_time.'--'.$end_time.'--'.$room_text."】";
                $room_id_str = $room_id_str.','.$room_id;  //教室
                $room_text_str = $room_text_str.','.$room_text; //教室名称
                $week_record = $week_record.','.$weekday_mysql;  //星期
                $time_slot = $time_slot.','.$start_time.'-'.$end_time; //开始结束时间
            }

            $room_id_str = trim($room_id_str);
            $room_text_str = trim($room_text_str);
            $week_record = trim($week_record);
            $time_slot = trim($time_slot);

            $room_id_str = substr($room_id_str, 1, strlen($room_id_str));
            $room_text_str = substr($room_text_str, 1, strlen($room_text_str));
            $week_record = substr($week_record, 1, strlen($week_record));
            $time_slot = substr($time_slot, 1, strlen($time_slot));

            $class_time_place = $str;
            $already_class = "";//已排次数
            $already_arranged = "";//上课进度，已上次数
            $status = ""; //0：未上课，1：上课中
            $public_end_date = "";
            $have_end_date_status = false; //表示设置在了结束日期

            if($class_end_date == ""){  //未填写结业日期
                $public_end_date =  NULL;
            }else{
                $public_end_date = $class_end_date;
                $have_end_date_status = true;
            }

            //保存gradesdetailModel数据
            $savedata = array(

                'campus_id' => $campus_id,
                'course_id' => $course_id,
                'grades_name' => $grades_name,
                'headmaster' => $headmaster,
                'preoccupation' => $preoccupation,
                'teacher' => $teacher,
                'assistant' => $assistant,
                'holiday_status' => $holiday_status,
                'class_start_date'=> $class_start_date,
                'class_end_date' => $public_end_date,
                'remarks' => $remarks,
                'repeat_status' => $repeat_status,
                'project_num' => $project_num,
                'interval_value' => $interval_value,
                'room_id' => $room_id_str,
                'week_record' => $week_record,
                'time_slot' => $time_slot,
                'room_text' => $room_text_str,
                'class_time_place' => $class_time_place,
                'user_master_id' => $user_master_id,
                'teacher_id' => $teacher_id,
                'assistant_id' => $assistant_id,
                'user_teacher_id' => $user_teacher_id,
                'user_assistant_id' => $user_assistant_id,
                'user_edu_id' => get_current_id(),  //教务ID

            );

            if(!$modify_status){
                if($this->gradesdetailModel->get(['grades_name' => $grades_name,'campus_id' =>$campus_id])){  //判断班级名称是否重复
                    $this->error('班级名字重复', null, -1, 1);
                }
            }


            $result = $this->gradesdetailModel->create($savedata);
            if (!$result) {
                    $this->error('排班失败', null, -1, 1);
                } else {
                    //插入班级详情表
                    $savedata_list = [];
                    $public_headmaster = $headmaster;  //公共班主任
                    $public_teacher = $teacher;  //公共任课老师
                    $public_assistant = $assistant;  //公共助教
                    $public_campus_id = $campus_id;  //公共校区
                    $public_course_id = $course_id;  //公共课程
                    $public_grades = $grades_name;  //公共班级名称


                    $init_day = "";
                    if($repeat_status == 1){
                        $init_day = $class_start_date; //开始日期
                    }else if($repeat_status == 2){
                        $init_day = $start_day_class; //开始日期
                    }
                    $timestamp = strtotime($init_day);

                for($num = 1; $num <= $project_num;) {


                    if($repeat_status == 1) {   //如果是按周重复
                        $postion = date("w",$timestamp);
                        $same_day = date("Y-m-d", $timestamp);
                        $public_end_date = $same_day;

                        if($have_end_date_status && strtotime($class_end_date) < strtotime($public_end_date) &&  $status_num == 2){  //填了结业日期

                            $sure_nums = $num-1; //可排次数
                            $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                            $this->success('最多可排【'.$sure_nums.'】次课', url('Grades/lists'), $sure_nums, 1);
                        }else if($have_end_date_status && strtotime($class_end_date) < strtotime($public_end_date)  &&  $status_num == 1){
                            $already_class =  $num-1; //可排次数;
                            break;
                        }


                        for($i = 0 ; $i < count($json_str); $i ++) {
                            $item = $json_str[$i];
                            $weekday = $item['weekday'];
                            if($weekarray[$postion] == $weekday) {
                                if($num -1 >= $project_num){
                                    $already_class =  $num-1; //可排次数;
                                    break;
                                }
                                //判断是否跳过节假日
                                if($holiday_status == 1){  //跳过节假日
                                    if(in_array($same_day,$this->holiday_arr)){
                                        break;
                                    }
                                }

                                $data = array();
                                $data['class_manage_id'] = $result['id'];
                                $data['headmaster'] = $public_headmaster;
                                $data['teacher'] = $public_teacher;
                                $data['assistant'] = $public_assistant;
                                $data['course_id'] = $public_course_id;
                                $data['campus_id'] = $public_campus_id;
                                $data['class_grades_name'] = $public_grades;
                                $data['class_date'] = $same_day;
                                $data['weekday'] = $item['weekday'];
                                $data['class_room_id'] = $item['room_id'];
                                $data['class_subject_start'] = $item['start_time'];
                                $data['class_subject_end'] = $item['end_time'];
                                $data['class_room'] = $item['room_text'];

                                //判断冲突
                                if($this->judgement_conflict($data, $modify_status, $manage_id, $public_campus_id ) == 1 && $conflict == 2){
                                    //助教冲突
                                    $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                                    $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---助教*【'.$public_assistant.'】冲突';
                                    $this->success($msg, url('Grades/lists'), 10001, 1);
                                }else if($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 2 && $conflict == 2){
                                    //老师冲突
                                    $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                                    $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---任课老师*【'.$public_teacher.'】冲突';
                                    $this->success($msg, url('Grades/lists'), 10001, 1);
                                }else if($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 3 && $conflict == 2){
                                    //教室冲突
                                    $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                                    $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---教室*【'. $item['room_text'].'】冲突';
                                    $this->success($msg, url('Grades/lists'), 10001, 1);
                                }else if(($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 1 || $this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 2 || $this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 3) && $conflict == 3){


                                }else{
                                    $savedata_list[] = $data;
                                    $num++;
                                }
                                /*
                                $savedata_list[] = $data;
                                $num ++;
                                */

                            }
                        }

                            if($postion == 0){  //周日
                                $days_add = $interval_value * 7;
                                $timestamp = strtotime('+'.$days_add.'days', $timestamp);
                            }
                            $timestamp = strtotime('+1days', $timestamp);


                    }else if($repeat_status == 2) {  //如果是按天重复

                        $same_day = date("Y-m-d", $timestamp);
                        $public_end_date = $same_day;


                        if ($have_end_date_status && strtotime($class_end_date) < strtotime($public_end_date) && $status_num == 2) {  //填了结业日期
                            $sure_nums = $num - 1; //可排次数
                            $this->gradesdetailModel->where(['id' => $result['id']])->delete();
                            $this->success('最多可排【' . $sure_nums . '】次课', url('Grades/lists'), $sure_nums, 1);
                        } else if ($have_end_date_status && strtotime($class_end_date) < strtotime($public_end_date) && $status_num == 1) {
                            $already_class = $num - 1; //可排次数;
                            break;
                        }


                        $weekday = $weekarray[date("w", $timestamp)];
                        //判断是否跳过节假日
                        $bool_holiday = false;
                        if ($holiday_status == 1) {  //跳过节假日
                            if (in_array($same_day, $this->holiday_arr)) {
                                /*
                                $timestamp = strtotime('+1days', $timestamp);
                                */
                                $bool_holiday = true;
                            }
                        }

                  if (!$bool_holiday) {
                        $data = array();
                        $data['class_manage_id'] = $result['id'];
                        $data['headmaster'] = $public_headmaster;
                        $data['teacher'] = $public_teacher;
                        $data['assistant'] = $public_assistant;
                        $data['course_id'] = $public_course_id;
                        $data['campus_id'] = $public_campus_id;
                        $data['class_grades_name'] = $public_grades;
                        $data['class_date'] = $same_day;
                        $data['weekday'] = $weekday;
                        $data['class_room_id'] = $item['room_id'];
                        $data['class_subject_start'] = $item['start_time'];
                        $data['class_subject_end'] = $item['end_time'];
                        $data['class_room'] = $item['room_text'];

                      //判断冲突
                      if($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 1 && $conflict == 2){
                          //助教冲突
                          $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                          $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---助教*【'.$public_assistant.'】冲突';
                          $this->success($msg, url('Grades/lists'), 10001, 1);
                      }else if($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 2 && $conflict == 2){
                          //老师冲突
                          $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                          $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---任课老师*【'.$public_teacher.'】冲突';
                          $this->success($msg, url('Grades/lists'), 10001, 1);
                      }else if($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 3 && $conflict == 2){
                          //教室冲突
                          $this->gradesdetailModel->where(['id'=> $result['id']])->delete();
                          $msg = '忽略--【'.$same_day.'】--时间段:【'.$item['start_time'].'--'.$item['end_time'].'】---教室*【'. $item['room_text'].'】冲突';
                          $this->success($msg, url('Grades/lists'), 10001, 1);
                      }else if(($this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 1 || $this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 2 || $this->judgement_conflict($data,$modify_status, $manage_id, $public_campus_id ) == 3) && $conflict == 3){


                      }else{
                          $savedata_list[] = $data;
                          $num++;
                      }

                     }


                        $days_add = $interval_value * 1;
                        $timestamp = strtotime('+' . $days_add . 'days', $timestamp);
                        $timestamp = strtotime('+1days', $timestamp);

                    }

                }

                $already_class = $num - 1; //可排次数;

                if($class_end_date == ""){
                    $this->gradesdetailModel->save(['class_end_date' => $public_end_date],['id' => $result['id']]);
                    $already_class = $project_num;
                }

                $this->gradesdetailModel->save(['already_class' => $already_class],['id' => $result['id']]); //更新可排次数

                if(count($savedata_list) > 0) {
                    if (!$this->gradesdetaillistModel->insertAll($savedata_list)) {
                        $this->error('排班失败', null, -1, 1);
                    }
                }


            }
            if($modify_status){
                $this->gradesdetailModel->where(['id' => $manage_id])->delete();
                $this->gradesdetaillistModel->where(['class_manage_id' => $manage_id])->delete();
                $this->success('修改成功', url('Grades/lists'), -1, 1);
            }
            $this->success('排班成功', url('Grades/lists'), -1, 1);

        }

    }

    /****
     * 排课冲突判断
     */
    public function judgement_conflict($course_item,$modify_status, $manage_id, $public_campus_id){

        $status_course = false;
        $current_campus_id = $public_campus_id; //当前校区

        for($a = 0; $a < count($this->course_lists_arr); $a ++){

            if($modify_status && $this->course_lists_arr[$a]['class_manage_id'] == $manage_id){
                continue;
            }

            if($this->course_lists_arr[$a]['campus_id'] == $current_campus_id){  //校区是否相同
                    if($this->course_lists_arr[$a]['class_date'] == $course_item['class_date']){ //日期是否相同

                        $course_list_start_timestamp = strtotime($this->course_lists_arr[$a]['class_subject_start']);  //课程开始时间
                        $course_list_end_timestamp = strtotime($this->course_lists_arr[$a]['class_subject_end']);  //课程结束时间
                        $course_item_start_timestamp = strtotime($course_item['class_subject_start']);  //课程开始时间
                        $course_item_end_timestamp = strtotime($course_item['class_subject_end']);  //课程结束时间

                        if($this->course_lists_arr[$a]['class_room_id'] == $course_item['class_room_id']){  //教室是否相同

                            //判断教室是否冲突
                            if($course_item_end_timestamp <= $course_list_start_timestamp){
                                if($this->course_lists_arr[$a]['teacher'] == $course_item['teacher']){  //任课老师是否相同
                                    //1.判断老师时间是否冲突
                                    //2.如果老师时间不冲突则比较助教是否相
                                    //3.比较助教相同的条件下时间是否冲突

                                    if($course_item_end_timestamp <= $course_list_start_timestamp){
                                        //任课老师时间不冲突
                                        if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                            if($course_item_end_timestamp <= $course_list_start_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else{

                                                //不可排
                                                $status_course = true;
                                                return  1;   //助教时间冲突
                                            }


                                        }else{
                                            //可排
                                            $status_course = false;
                                        }

                                    }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                        //任课老师时间不冲突
                                        if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                            if($course_item_end_timestamp <= $course_list_start_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else{

                                                //不可排
                                                $status_course = true;
                                                return  1;   //助教时间冲突
                                            }



                                        }else{
                                            //可排
                                            $status_course = false;
                                        }


                                    }else{
                                        //不可排
                                        $status_course = true;
                                        return  2;   //老师时间冲突
                                    }



                                }else if($this->course_lists_arr[$a]['teacher'] != $course_item['teacher']){
                                    if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                        //比时间
                                        if($course_item_end_timestamp <= $course_list_start_timestamp){
                                            //可排
                                            $status_course = false;

                                        }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                            //可排
                                            $status_course = false;

                                        }else{
                                            //不可排
                                            $status_course = true;
                                            return  1;   //助教时间冲突
                                        }

                                    }

                                }

                            }else if($course_item_start_timestamp >= $course_list_end_timestamp){

                                if($this->course_lists_arr[$a]['teacher'] == $course_item['teacher']){  //任课老师是否相同
                                    //1.判断老师时间是否冲突
                                    //2.如果老师时间不冲突则比较助教是否相
                                    //3.比较助教相同的条件下时间是否冲突

                                    if($course_item_end_timestamp <= $course_list_start_timestamp){
                                        //任课老师时间不冲突
                                        if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                            if($course_item_end_timestamp <= $course_list_start_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else{

                                                //不可排
                                                $status_course = true;
                                                return  1;   //助教时间冲突
                                            }


                                        }else{
                                            //可排
                                            $status_course = false;
                                        }

                                    }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                        //任课老师时间不冲突
                                        if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                            if($course_item_end_timestamp <= $course_list_start_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                                //可排
                                                $status_course = false;
                                            }else{

                                                //不可排
                                                $status_course = true;
                                                return  1;   //助教时间冲突
                                            }



                                        }else{
                                            //可排
                                            $status_course = false;
                                        }


                                    }else{
                                        //不可排
                                        $status_course = true;
                                        return  2;   //老师时间冲突
                                    }



                                }else if($this->course_lists_arr[$a]['teacher'] != $course_item['teacher']){
                                    if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                        //比时间
                                        if($course_item_end_timestamp <= $course_list_start_timestamp){
                                            //可排
                                            $status_course = false;

                                        }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                            //可排
                                            $status_course = false;

                                        }else{
                                            //不可排
                                            $status_course = true;
                                            return  1;   //助教时间冲突
                                        }

                                    }

                                }
                            }else{
                                //不可排
                                $status_course = true;
                                return  3;   //教室时间冲突
                            }


                        }else if($this->course_lists_arr[$a]['class_room_id'] != $course_item['class_room_id']){  //教室不同
                            if($this->course_lists_arr[$a]['teacher'] == $course_item['teacher']){  //任课老师是否相同
                                //1.判断老师时间是否冲突
                                //2.如果老师时间不冲突则比较助教是否相同
                                //3.比较助教相同的条件下时间是否冲突
                                if($course_item_end_timestamp <= $course_list_start_timestamp){
                                    //任课老师时间不冲突
                                    if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                        if($course_item_end_timestamp <= $course_list_start_timestamp){
                                            //可排
                                            $status_course = false;
                                        }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                            //可排
                                            $status_course = false;
                                        }else{

                                            //不可排
                                            $status_course = true;
                                            return  1;   //助教时间冲突
                                        }


                                    }else{
                                        //可排
                                        $status_course = false;
                                    }

                                }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                    //任课老师时间不冲突
                                    if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){
                                        if($course_item_end_timestamp <= $course_list_start_timestamp){
                                            //可排
                                            $status_course = false;
                                        }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                            //可排
                                            $status_course = false;
                                        }else{

                                            //不可排
                                            $status_course = true;
                                            return  1;   //助教时间冲突
                                        }



                                    }else{
                                        //可排
                                        $status_course = false;
                                    }


                                }else{
                                    //不可排
                                    $status_course = true;
                                    return  2;   //老师时间冲突
                                }



                            }else if($this->course_lists_arr[$a]['teacher'] != $course_item['teacher']){
                                if($this->course_lists_arr[$a]['assistant'] == $course_item['assistant']){  //助教老师是否相同
                                                                        //比时间是否冲突
                                    if($course_item_end_timestamp <= $course_list_start_timestamp){
                                        //可排
                                        $status_course = false;

                                    }else if($course_item_start_timestamp >= $course_list_end_timestamp){
                                        //可排
                                        $status_course = false;

                                    }else{
                                        //不可排
                                        $status_course = true;
                                        return  1;   //助教时间冲突
                                    }


                                }
                            }

                        }

                    }

            }


        }


        return -1;
    }


    /**
     * 编辑班主任.
     *
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (!$this->gradesdetailModel->save($data,['id'=>$data['id']])) {
                $this->error('修改班主任失败', null, '', 1);
            }
            $this->success('修改班主任成功', url('Campus/lists'), '', 1);
        }
    }

    /**
     * 结业.
     *
     * @return mixed
     */
    public function del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {

            //结业学员
            $grades_result = $this->studentsubjectinfosclassModel->where(['class_subject_id' => input('id')])->select();
            $student_arr = [];
            foreach ($grades_result as $key => $value){
                if($value['contract_number'] != null && $value['contract_number'] != '---'){
                    $item = [];
                    $item['id'] = $value['student_id'];
                    $item['conclusion_status'] = 1;
                    $student_arr[] = $item;

               }
            }
            if(count($student_arr) > 0){
                $this->studentModel->saveAll($student_arr);  //更新状态
            }

            if (!$this->gradesdetailModel->save(['graduation_status' => 1], ['id'=>input('id')])) {
                $this->error('更新班课状态失败', null, '', 1);
            }

            if (!$this->gradesdetaillistModel->where(['class_manage_id' => input('id')])->update(['graduation_status' => 1])) {
                $this->error('更新节课状态失败', null, '', 1);
            }

            /*
            if($this->gradesdetaillistModel->where(['class_manage_id'=> input('id')])->count() > 0){
                if(!$this->gradesdetaillistModel->where(['class_manage_id'=> input('id')])->delete()){
                    $this->error('删除班课列表失败', null, '', 1);
                }
            }
                if($this->studentsubjectclassModel->where(['class_subject_id' => input('id')])->count() > 0){
                    if(!$this->studentsubjectclassModel->where(['class_subject_id' => input('id')])->delete()){
                        $this->error('删除学生分配表失败', null, '', 1);
                    }
                }
                    if($this->studentsubjectinfosclassModel->where(['class_subject_id' => input('id')])->count() > 0){
                        if(!$this->studentsubjectinfosclassModel->where(['class_subject_id' => input('id')])->delete()){
                            $this->error('删除节课分配表失败', null, '', 1);
                        }
                    }
            */

            $this->success('该课程结业成功', '', '', 1);
        }
    }

    /**
     * 更改校区状态.
     *
     * @return mixed
     */
    public function classmanage_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if($data['status'] == 1){
                $msg = '启用';
            }else{
                $msg = '禁用';
            }
            if (!$this->gradesModel->save($data,['id'=>$data['id']])) {
                $this->error($msg.'校区失败', null, '', 1);
            }
            $this->success($msg.'成功', '', '', 1);
        }
    }

    /***
        *分班
     *
     */

    public function class_assignment(){
        $data = [];
		$class_id = $room_id = $campus_id = '';
        if(isset($_GET["campus_id"]) && isset($_GET["course_id"]) && isset($_GET["course_name"]) && isset($_GET["id"]) && isset($_GET["room_id"])){
            $course_id = input('course_id'); //课程id
            $course_name = input('course_name'); //课程名称
            $class_id = input('id'); //班级ID
            $room_id = input('room_id'); //教室ID
            $campus_id = input('campus_id');

            for($k =0; $k < count($this->course_assignment_arr); $k ++){
                if(strpos($this->course_assignment_arr[$k]['course'],''.$course_id) !== false){//包含
                    $result = $this->contractModel->get(['contract_number' => $this->course_assignment_arr[$k]['contract_number']]);
                    $item = [];
                    if($this->course_assignment_arr[$k]['record_status'] == 1){ //课程
                        $explode_arr = explode(",",$result['course']);
                        $explode_elimination_arr = explode(",",$result['course_elimination']);
                        $explode_elimination_arr_give = explode(",",$result['give_course_nums']);
                        for($j = 0; $j < count($explode_arr); $j ++){
                            if($explode_arr[$j] == $course_id){
                                $item['course_elimination'] = $explode_elimination_arr[$j];
                                $item['course_elimination_give'] = $explode_elimination_arr_give[$j];
                                break;
                            }

                        }
                        $res = $this->studentsubjectclassModel->get(['contract_number' => $this->course_assignment_arr[$k]['contract_number'],  'status' => 1, 'class_subject_id' => $class_id]);
                        if($res){
                            $item['status_match'] = 1;
                        }else{
                            $item['status_match'] = 0;
                        }
                        $item['student_name'] = $result['student_name'];
                        $item['id'] = $this->course_assignment_arr[$k]['id'];
                        $item['course'] = $this->course_assignment_arr[$k]['course'];
                        $item['campus'] = $this->course_assignment_arr[$k]['campus'];
                        $item['type'] = 1;
                        $item['contract_number'] = $this->course_assignment_arr[$k]['contract_number'];
                        $item['student_id'] = $result['student_id'];
                        if($result['status'] == 1){
                            $item['status_text'] = '已付款';
                        }else if($result['status'] == 2){
                            $item['status_text'] = '未付款';
                        }else if($result['status'] == 3){
                            $item['status_text'] = '部分付款';
                        }
                        $item['course_name'] = $course_name.'(课程)';
                        $data[] = $item;

                    }else if($this->course_assignment_arr[$k]['record_status'] == 2){  //班级
                        $explode_arr = explode(",",$result['grades_course']);
                        $explode_elimination_arr = explode(",",$result['grades_course_elimination']);
                        $explode_elimination_arr_give = explode(",",$result['give_grades_course_nums']);
                        for($j = 0; $j < count($explode_arr); $j ++){
                            if($explode_arr[$j] == $course_id){
                                $item['course_elimination'] = $explode_elimination_arr[$j];
                                $item['course_elimination_give'] = $explode_elimination_arr_give[$j];
                                break;
                            }

                        }
                        $res = $this->studentsubjectclassModel->get(['contract_number' => $this->course_assignment_arr[$k]['contract_number'],  'status' => 2, 'class_subject_id' => $class_id]);
                        if($res){
                            $item['status_match'] = 1;
                        }else{
                            $item['status_match'] = 0;
                        }
                        $item['student_name'] = $result['student_name'];
                        $item['id'] = $this->course_assignment_arr[$k]['id'];

                        $item['course'] = $this->course_assignment_arr[$k]['course'];
                        $item['campus'] = $this->course_assignment_arr[$k]['campus'];
                        $item['type'] = 2;
                        $item['contract_number'] = $this->course_assignment_arr[$k]['contract_number'];
                        $item['student_id'] = $result['student_id'];
                        if($result['status'] == 1){
                            $item['status_text'] = '已付款';
                        }else if($result['status'] == 2){
                            $item['status_text'] = '未付款';
                        }else if($result['status'] == 3){
                            $item['status_text'] = '部分付款';
                        }
                        $grades_name = $this->gradesdetailModel->get(['id' => $class_id])['grades_name'];
                        $item['course_name'] = $grades_name.'【'.$course_name.'】'.'(班级)';
                        $data[] = $item;
                    }

                }
            }

        }
        $json = json_encode($data);
        return $this->fetch('',[
            'jsonmsg' => $json,  //返回json信息
            'class_id' => $class_id, //班级ID
            'room_id'=>$room_id,//教室
            'campus_id'=>$campus_id
        ]);
    }


    /**
    *插入学生表
     */

    public function insert_student_table(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $studentdata = $data['studentdata'];  //分班学生数据
            $studentdata_delete = $data['studentdata_delete'];  //需要删除已存在的分班学生数据
            $class_id = $data['class_id'];//班级ID
            $room_id = $data['room_id'];  //教室字符串id
            $campus_id = $data['campus_id'];
            $room_classroom = explode(",", $room_id); //获取教室列表
            $room_id_classroom = [];  //教室id
            for ($a = 0; $a < count($room_classroom); $a++) {
                if (!in_array($room_classroom[$a], $room_id_classroom)) {
                    $room_id_classroom[] = $room_classroom[$a];
                }
            }

            $save_data = [];
            $json_student_data = json_decode($studentdata, true);
            $json_student_delete_data = json_decode($studentdata_delete, true);  //需要删除的已分班的学生数据
            if (count($json_student_data) > 0) {
                for ($i = 0; $i < count($json_student_data); $i++) {
                    $item = $json_student_data[$i];
                    for ($j = 0; $j < count($room_id_classroom); $j++) {
                        if($item['status_match'] == 0) {  //先前表里不存在
                            $cell = [];
                            $cell['student_id'] = $item['student_id'];  //学生ID
                            $cell['student_name'] = $item['student_name']; //学生姓名
                            $cell['room_id'] = $room_id_classroom[$j];//教室ID
                            $cell['class_subject_id'] = $class_id;//班级ID
                            $cell['campus_id'] = $campus_id;
                            if($item['type'] == 1){
                                $cell['status'] = 1;//课程
                            }else if($item['type'] == 2){
                                $cell['status'] = 2;//班级
                            }
                            $cell['contract_number'] = $item['contract_number']; //合同编号
                            $save_data[] = $cell;
                        }
                    }

                }

            }

            $delete_student_id = "";
            $delete_student_type = "";
            $delete_student_contract_number = "";
            if(count($json_student_delete_data) > 0){
                for ($i = 0; $i < count($json_student_delete_data); $i++) {
                    $item = $json_student_delete_data[$i];
                    $delete_student_id = $delete_student_id.$item['student_id'].',';
                    $delete_student_type = $delete_student_type.$item['type'].',';
                    $delete_student_contract_number = $delete_student_contract_number.$item['contract_number'].',';
                }


                $delete_student_id = substr($delete_student_id, 0, strlen($delete_student_id)-1);
                $delete_student_type = substr($delete_student_type, 0, strlen($delete_student_type)-1);
                $delete_student_contract_number = substr($delete_student_contract_number, 0, strlen($delete_student_contract_number)-1);
                $where = [];
                $where['student_id'] = ['in',$delete_student_id];
                $where['class_subject_id'] = $class_id;
                $where['status'] = ['in',$delete_student_type];
                $where['contract_number'] = ['in',$delete_student_contract_number];
                if(!$this->studentsubjectclassModel->where($where)->delete()){
                    $this->error('删除异常', null, -1, 1);
                }
                if($this->studentsubjectinfosclassModel->where("student_id in ($delete_student_id) AND class_subject_id=$class_id AND status = 0")->count() > 0){
                    $infos_result = $this->studentsubjectinfosclassModel->where("student_id in ($delete_student_id) AND class_subject_id=$class_id AND status = 0")->select();
                    foreach ($infos_result as $key => $value){
                            if($value['follow_status'] == 2){   //跟班
                                    $this->studentsubjectinfosclassModel->where(['id' => $value['lessons_id']])->update(['lessons_status' => 0]);
                            }
                    }
                    if(!$this->studentsubjectinfosclassModel->where("student_id in ($delete_student_id) AND class_subject_id=$class_id AND status = 0")->delete()){
                        $this->error('排课删除异常', null, -1, 1);
                    }


                }

            }

            if(count($json_student_data) > 0){
                $sava_cell_date = [];
                $resullt = $this->gradesdetaillistModel->where(['class_manage_id' => $class_id])->select();
                foreach ($resullt as $key => $value){
                    $id = $value['id'];
                    $room_id = $value['class_room_id']; //教室ID
                    $room_name = $value['class_room']; //教室名称
                    $class_date = $value['class_date'];//开课日期
                    $weekday = $value['weekday'];//星期
                    $course_id = $value['course_id']; //课程ID
                    $campus_id =  $value['campus_id']; //校区ID
                    $start = strtotime($value['class_subject_start']);  //开始时间
                    $end = strtotime($value['class_subject_end']);  //结束时间
                    $duration = ceil(($end - $start)/60); //节课时长
                    for ($i = 0; $i < count($json_student_data); $i++) {
                        $item = $json_student_data[$i];
                        if($item['status_match'] == 0) {  //先前表里不存在
                            $cell = [];
                            $cell['contract_number'] = $item['contract_number'];  //订单编号ID，非自增ID
                            $cell['student_id'] = $item['student_id'];  //学生ID
                            $cell['student_name'] = $item['student_name']; //学生姓名
                            $cell['surplus_class_elimination'] = $item['course_elimination']; //剩余课时
                            $cell['surplus_class_elimination_give'] = $item['course_elimination_give']; //赠送课时
                            $cell['room_id'] = $room_id;//教室ID
                            $cell['class_subject_id'] = $class_id;//班级ID
                            $cell['class_manage_details_id'] = $id; //ID
                            $cell['room_name'] = $room_name; //教室名称
                            $cell['class_date'] = $class_date; //开班日期
                            //操作人
                            $cell['operatorer'] = get_current_name();  //操作人
                            if($item['type'] == 1){
                                $cell['report_status'] = 1;//课程
                            }else if($item['type'] == 2){
                                $cell['report_status'] = 2;//班级
                            }
                            $cell['course_id'] = $course_id; //课程ID
                            $cell['duration'] = $duration; //上课时长
                            $cell['weekday'] = $weekday; //星期
                            $cell['campus_id'] = $campus_id; //校区ID
                            $sava_cell_date[] = $cell;
                        }
                    }

                }

                if(count($sava_cell_date) > 0){
                    if (!$this->studentsubjectinfosclassModel->insertAll($sava_cell_date)) {
                        $this->error('排课分班失败', null, -1, 1);
                    }

                }else{

                    $this->error('该班级没有排课', null, -1, 1);
                }

                if (!$this->studentsubjectclassModel->insertAll($save_data)) {
                    $this->error('分班失败', null, -1, 1);
                }
            }

            //修改开班状态
            $result = $this->studentsubjectclassModel->where(['class_subject_id' => $class_id])->select();
            if(count($result) > 0){
                if(!$this->gradesdetailModel->update(['status' => 1],['id' => $class_id])){
                    $this->error('更新开班记录失败', null, -1, 1);
                }

            }else{
                if(!$this->gradesdetailModel->update(['status' => 0],['id' => $class_id])){
                    $this->error('更新开班记录失败', null, -1, 1);
                }
            }

            $this->success('学生分班成功', '', '', 1);
        }
    }


    /***选择要分配的教室**/
    public function choice_room(){
        $room_json = [];
        if(isset($_GET["id"])) {
            $class_id = input('id'); //教室id
            $result = $this->gradesdetailModel->get(['id' => $class_id]);
            $class_room_id = $result['room_id'];
            $room_arr = explode(',',$class_room_id);
            for ($i = 0 ; $i < count($room_arr); $i ++){
                $res = $this->classRoomModel->get(['id' => $room_arr[$i]]);
                if($res['status'] == 0){
                    continue;
                }
                $status =false;
                for ($a = 0; $a < count($room_json); $a ++){
                    if($room_json[$a]['room_id'] == $room_arr[$i]){
                        $status = true;
                        break;
                    }
                }
                if(!$status){
                    $item = [];
                    $item['room_id'] = $room_arr[$i];
                    $item['room_name'] = $this->classRoomModel->get(['id' => $room_arr[$i]])['class_name'];
                    $room_json[] = $item;
                }
            }
        }
        $json = json_encode($room_json);
        return $this->fetch('',[
        	'room_json' => $json,
		]);
    }


    /**
     *  实例化.
     * @return bool|void
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->gradesModel = new GradesModel();
        $this->courseModel = new CourseModel();
        $this->campusModel = new CampusModel();
        $this->teacherModel = new TeacherModel();
        $this->classRoomModel = new ClassRoomModel();
        $this->classTimeModel = new ClassTimeModel();
        $this->classRoomTimeModel = new ClassRoomTimeModel();
        $this->arrangingModel = new ArrangingModel();
        $this->gradesdetailModel = new GradesDetailsModel();
        $this->gradesdetaillistModel = new GradesDetailsListsModel();
        $this->holidayModel = new HolidayModel();
        $this->courselogModel = new CourseLogModel();
        $this->contractModel = new ContractModel();
        $this->studentsubjectclassModel = new StudentSubjectClassModel();
        $this->studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
        $this->studentModel = new StudentModel();

        //获取节假日信息
        if(count($this->holiday_arr) <= 0){
            $result_msg = $this->holidayModel->where(['company_id'=>get_current_company_id()])->select();
            foreach ($result_msg as $key=>$value){
                $this->holiday_arr[] = $value['date_name'];
            }
         }


         //排课详情信息
            $result_course_lists = $this->gradesdetaillistModel->where(['campus_id' => ['in', get_current_campus_id()]])->select();
            array_splice($this->course_lists_arr, 0, count($this->course_lists_arr));  //清空数组
            foreach ($result_course_lists as $key=>$value){
                    $item = [];
                    $item['class_date'] = $value['class_date']; //开课日期
                    $item['class_manage_id'] = $value['class_manage_id']; //教室管理详情表自增id
                    $item['course_id'] = $value['course_id']; //课程ID
                    $item['campus_id'] = $value['campus_id']; //归属校区
                    $item['weekday'] = $value['weekday']; //星期
                    $item['headmaster'] = $value['headmaster']; //班主任
                    $item['teacher'] = $value['teacher']; //任课老师
                    $item['assistant'] = $value['assistant']; //助教
                    $item['class_grades_name'] = $value['class_grades_name']; //班级名称
                    $item['class_subject_start'] = $value['class_subject_start']; //上课开始时间
                    $item['class_subject_end'] = $value['class_subject_end']; //上课结束时间
                    $item['class_room_id'] = $value['class_room_id']; //教室id
                    $item['class_room'] = $value['class_room']; //教室名称
                    $item['status'] = $value['status']; //上课状态
                    $this->course_lists_arr[] = $item;
            }


             //已报课程详情列表
            $result_assignment_arr = $this->courselogModel->order('create_time desc')->select();
            array_splice($this->course_assignment_arr, 0, count($this->course_assignment_arr));  //清空数组
            foreach ($result_assignment_arr as $key=>$value){
                $item = [];
                $item['id'] = $value['id']; //id
                $item['course'] = $value['course']; //课程
                $item['campus'] = $value['campus']; //校区
                $item['contract_number'] = $value['contract_number']; //订单编号
                $item['status'] = $value['status']; //付款状态
                $item['record_status'] = $value['record_status']; //报读来源
                $this->course_assignment_arr[] = $item;

            }
    }
}
